<?php

namespace app\admin\controller;

class Information extends Base
{
    //资讯列表
    public function information()
    {
        $infor = model('common/Information') -> order('create_time','desc') -> paginate(8);
        $data = [
            'infor' => $infor
        ];
        $this -> assign($data);
        return view();
    }

    //资讯添加
    public function information_add()
    {
        if (request() -> isAjax()){
            $data = input('post.');
            $data['img'] = request()->file('myFile');
            $result = model('common/Information') -> information_add($data);
            if ($result == 1){
                $this -> success('资讯添加成功','admin/information/information');
            } else {
                $this -> error($result);
            }
        }
        return view();
    }

    //资讯编辑
    public function information_edit()
    {
        if (request()->isAjax()){
            $data = input('post.');
            $data['imgs'] = request()->file('myFile');
            $result = model('common/Information') -> information_edit($data);
            if ($result == 1){
                $this -> success('更新成功！','admin/information/information');
            } else {
                $this -> error('更新失败！');
            }
        }
        $infor = model('common/Information') -> find(input('id'));
        $data = [
            'infor' => $infor
        ];
        $this -> assign($data);
        return view();
    }

    //资讯删除
    public function information_del()
    {
        if (request()->isAjax()){
            $infor = model('common/Information') -> find(input('post.id'));
            $result = $infor -> delete();
            if ($result){
                $this -> success('删除成功！','admin/information/information');
            } else {
                $this -> error('删除失败！');
            }
        } else {
            return '非法请求';
        }
    }
}
